package Phase_Sixth_Module_One

case class userInfo(name: String, location: String, startTime: Int, duration: Int)

object Homework_Three {

  def main(args: Array[String]): Unit = {

    val userInfoList = List(
      userInfo("UserA", "LocationA", 9, 60),
      userInfo("UserA", "LocationA", 8, 60),
      userInfo("UserB", "LocationB", 11, 80),
      userInfo("UserB", "LocationB", 10, 60)
    )

    val userInfoMap = userInfoList.groupBy(x => x.name + "," + x.location)
    println(userInfoMap)
//    Map(UserB,LocationB -> List(userInfo(UserB,LocationB,11,80), userInfo(UserB,LocationB,10,60)), UserA,LocationA -> List(userInfo(UserA,LocationA,9,60), userInfo(UserA,LocationA,8,60)))

    val sortByInfoMap = userInfoMap.mapValues(x => x.sortBy(y => y.startTime))
    println(sortByInfoMap)
//    Map(UserB,LocationB -> List(userInfo(UserB,LocationB,10,60), userInfo(UserB,LocationB,11,80)), UserA,LocationA -> List(userInfo(UserA,LocationA,8,60), userInfo(UserA,LocationA,9,60)))
    var firstTime = 0
    var sum = 0
    val endMap = sortByInfoMap.mapValues(x => {
      firstTime = x.head.startTime
      sum = x.map(y => y.duration).sum
    })
    endMap.foreach {
      case (a, b) => println(s"$a,$firstTime,$sum")
    }
  }
}
